<?php
declare (strict_types=1);

namespace app\common\model;

use think\Model;

//系统配置数据表
class SysConfModel extends Model
{
    protected $table = 'utf8mb4_sys_conf';
    public $error = '';
    public $rule = [
        'name' => 'require|max:20',
        'key' => 'require|max:20|unique:utf8mb4_sys_conf',
        'value' => 'require|max:255',
        'type' => 'require|in:1,2,3',
        'desc' => 'require|max:255'
    ];

    public $message = [
        'name.require' => '名称不能为空',
        'name.max' => '名称最长20个字符',
        'key.require' => 'key不能为空',
        'key.max' => 'key最长20个字符',
        'key.unique' => 'key已经添加过了',
        'value.require' => 'key不能为空',
        'value.max' => 'val最大255个字符',
        'type.require' => '类型不能为空',
        'type.in' => '类型选择错误',
        'desc.require' => '简介不能为空',
        'desc.max' => '简介最多255个字符'
    ];

    public $type_list = [
        1 => 'string',
        2 => 'json',
        3 => 'number'
    ];

    //检测值的类型
    public function checkType(array $param)
    {
        switch ($param['type']) {
            case '1':
                if (is_numeric($param['value'])) {
                    $this->error = '数据类型是一个数字';
                    return false;
                }
                break;
            case '2':
                $info = json_decode($param['value']);
                if (!is_array($info)) {
                    $this->error = '错误的json数据';
                    return false;
                }
                break;
            case '3':
                if (!is_numeric($param['value'])) {
                    $this->error = '数据类型不是一个数字';
                    return false;
                }
                break;
        }
        return true;
    }

}